package user.view;



import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JPasswordField;

import user.model.UserModel;
import user.ucm.UCRegistering;

import db.UserValidate;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RegisterView extends JDialog {

	private JPanel contentPane;
	private JTextField user;
	private JPasswordField passwordField;
	private JPasswordField confirmField;
	private JTextField address_field;
	private JTextField phone_text;
	private JLabel check_label;
	private JLabel warn_label;
	private JLabel res_label;
	private static RegisterView inst;
	private int usertype;
	public static RegisterView getInstance()
	{
		if(inst==null)inst=new RegisterView();
		return inst;
	}
	public void setUsertype(int type)
	{
		this.usertype=type;
	}
	/**
	 * Launch the application.
	 */


	/**
	 * Create the frame.
	 */
	private RegisterView() {
		setTitle("Register in");
		setModal(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 348);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		{
			JLabel user = new JLabel("Username(*)");
			user.setBounds(10, 40, 80, 15);
			contentPane.add(user);
		}
		{
			user = new JTextField();
			user.setBounds(100, 37, 137, 21);
			contentPane.add(user);
			user.setColumns(10);
		}
		{
			JButton btnCheck = new JButton("check available");
			btnCheck.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String username=user.getText().trim();
					if(username.equals("")){
						check_label.setText("please input username!");
						return;
					}
					if(UserValidate.isExist(username,usertype))
					{
						check_label.setText("the username has been used!");
						
					}else {
						check_label.setText("congratulations! it's available");
					}
					
				}
			});
			btnCheck.setBounds(257, 36, 128, 23);
			contentPane.add(btnCheck);
		}
		{
			check_label = new JLabel("");
			check_label.setForeground(Color.BLUE);
			check_label.setBounds(100, 69, 285, 15);
			contentPane.add(check_label);
		}
		{
			JLabel pwd = new JLabel("Password(*)");
			pwd.setBounds(10, 100, 80, 15);
			contentPane.add(pwd);
		}
		{
			JLabel lblConfirm = new JLabel("Confirm(*)");
			lblConfirm.setBounds(10, 145, 80, 15);
			contentPane.add(lblConfirm);
		}
		{
			passwordField = new JPasswordField();
			passwordField.setBounds(100, 97, 137, 21);
			contentPane.add(passwordField);
		}
		{
			confirmField = new JPasswordField();
			confirmField.setBounds(100, 142, 137, 21);
			contentPane.add(confirmField);
		}
		{
			JLabel lblAddress = new JLabel("Address");
			lblAddress.setBounds(10, 222, 54, 15);
			contentPane.add(lblAddress);
		}
		{
			address_field = new JTextField();
			address_field.setBounds(100, 219, 285, 21);
			contentPane.add(address_field);
			address_field.setColumns(10);
		}
		{
			JLabel lblPhone = new JLabel("phone");
			lblPhone.setBounds(10, 185, 54, 15);
			contentPane.add(lblPhone);
		}
		{
			phone_text = new JTextField();
			phone_text.setBounds(100, 182, 137, 21);
			contentPane.add(phone_text);
			phone_text.setColumns(10);
		}
		{
			warn_label = new JLabel("");
			warn_label.setForeground(Color.RED);
			warn_label.setBounds(247, 145, 177, 15);
			contentPane.add(warn_label);
		}
		{
			JButton btnRegister = new JButton("Register");
			btnRegister.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String username=user.getText().trim();
					if(username.equals("")){
						check_label.setText("please input username!");
						return;
					}
					String pwd=new String(passwordField.getPassword());
					if(pwd.trim().equals(""))
					{
						warn_label.setText("please input password!");
						return;
					}
					String pwd1=new String(confirmField.getPassword());
					if(!pwd.equals(pwd1))
					{
						warn_label.setText("wrong confirm password!");
						return;
					}
					if(UserValidate.isExist(username,usertype))
					{
						check_label.setText("the username has been used!");
						return ;
					}
					UserModel um=new UserModel();
					um.setAddress(address_field.getText().trim());
					um.setPhone(phone_text.getText().trim());
					um.setType(usertype);
					um.setName(username);
					um.setPsw(pwd);
					UCRegistering registering=new UCRegistering(um);
					int rs=registering.run();
					if(rs==1){res_label.setText(username+" register sucessfully! ");
						passwordField.setText("");
						confirmField.setText("");
						warn_label.setText("");
						
					}
				}
			});
			btnRegister.setBounds(100, 265, 93, 23);
			contentPane.add(btnRegister);
		}
		{
			res_label = new JLabel("");
			res_label.setForeground(Color.RED);
			res_label.setBounds(214, 269, 210, 15);
			contentPane.add(res_label);
		}
	}
}
